package com.beiing.tianshuai.tianshuai.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.BGABannerUtil;
import cn.bingoogolapple.bgabanner.BGALocalImageSize;

/**
 * 引导页
 *
 * @author 刘泽启
 * @date 2017/10/18
 */

public class GuideActivity extends BaseActivity {

    @BindView(R.id.guide_banner)
    BGABanner mBanner;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    /**
     * 声明一个数组，用来存储所有需要动态申请的权限
     */
    @SuppressLint("InlinedApi")
    private String[] permissions = new String[]{
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.READ_PHONE_STATE};

    /**
     * 声明一个集合，在后面的代码中用来存储用户拒绝授权的权
     */
    private List<String> mPermissionList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_guide;
    }

    @Override
    protected void init() {
        SharedPreferences sp = this.getSharedPreferences("isFirstLaunch", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("isFirstEnter", true);
        editor.apply();
        initStatusBar();
        checkPermission();
        setBannerResource();
        setListener();
    }

    private void initStatusBar() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    private void setBannerResource() {
        List<View> views = new ArrayList<>();
        BGALocalImageSize bgaLocalImageSize = new BGALocalImageSize(
                1242, 2208,
                621, 1104
        );
        views.add(BGABannerUtil.getItemImageView(
                mContext, R.mipmap.guide_img_a, bgaLocalImageSize, ImageView.ScaleType.CENTER_CROP));
        views.add(BGABannerUtil.getItemImageView(
                mContext, R.mipmap.guide_img_b, bgaLocalImageSize, ImageView.ScaleType.CENTER_CROP));
        views.add(BGABannerUtil.getItemImageView(
                mContext, R.mipmap.guide_img_c, bgaLocalImageSize, ImageView.ScaleType.CENTER_CROP));
        mBanner.setData(views);
    }

    private void setListener() {
        mBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tvEnter.setVisibility(View.GONE);
                        break;
                    case 1:
                        tvEnter.setVisibility(View.GONE);
                        break;
                    case 2:
                        tvEnter.setVisibility(View.VISIBLE);
                        break;
                    default:
                        tvEnter.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        tvEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, MainActivity.class));
                finish();
            }
        });
    }

    private void checkPermission() {
        mPermissionList.clear();
        /*
         * 判断哪些权限未授予
         * 以便必要的时候重新申请
         */
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permission);
            }
        }

        // 判断存储委授予权限的集合是否为空
        if (!mPermissionList.isEmpty()) {
            //将List转为数组
            String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);
            ActivityCompat.requestPermissions(GuideActivity.this, permissions, 1);
        }
    }
}
